package cn.com.yang.serializable;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * 大数据常用序列化 hadoop子项目 apache
 *
 * @Author 杨亿金
 * @Date 2021/12/28 22:27
 * @CopyRight
 * @Version
 */
public class TestAvro {
    /***
     *Record：Enums：Maps：Fixed：Unions：Arrays：
     * {
     * "namespace": "com.jv.avro",
     *  "type": "record",
     *  "name": "User",
     *  "fields": [
     *      {"name": "username","type": "string"},
     *      {"name": "age","type": ["int", "null"]},
     *      {"name": "address","type": ["string", "null"]}
     *  ]
     * }
     *         RECORD,
     *         ENUM,
     *         ARRAY,
     *         MAP,
     *         UNION,
     *         FIXED,
     *         STRING,
     *         BYTES,
     *         INT,
     *         LONG,
     *         FLOAT,
     *         DOUBLE,
     *         BOOLEAN,
     *         NULL;
     *
     *
     *Avro序列化设计初衷是为了支持大批量数据交换的应用，
     * 支持二进制序列化方式，并且自身提供了动态语言支持，可以更加便捷、快速处理大批量的Avro数据
     */

    /**
     * 对象序列化
     *java.lang.Exception: Method deAvroserializable should have no parameters
     */
    @Test
    public void enAvroserializable() {
        // org.apache.avro.AvroRuntimeException: Not a Specific class: class cn.com.yang.serializable.AvroUser
        try {
            Customer c = new Customer(1001,"jom");
            //AvroUser user = new AvroUser("dfdsfds","ffff",123);
            //AvroUser user1 = new AvroUser.Builder(Schema.create(Schema.Type.RECORD)).userName("hiz").address("ass").age(29).build();
            DatumWriter<Customer> datumReader = new SpecificDatumWriter<Customer>(Customer.class);
            DataFileWriter<Customer> dataFileWriter = new DataFileWriter<Customer>(datumReader);
            dataFileWriter.create(c.getSchema(), new File("user.avro"));
            dataFileWriter.append(c);
            dataFileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 反序列化
     *
     * @throws IOException
     */
    @Test
    public void deAvroserializable() {
        String scameFile = "users.avro";
        DatumReader<AvroUser> userDatumReader = new SpecificDatumReader<AvroUser>(AvroUser.class);
        try {
            DataFileReader<AvroUser> dataFileReader = new DataFileReader<AvroUser>(new File(scameFile), userDatumReader);
            AvroUser user = null;
            while (dataFileReader.hasNext()) {

                user = dataFileReader.next(user);

                System.out.println(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
